<?php

namespace App\Http\Controllers\QH_Api;

use App\Http\Controllers\TK_Api\ApiBaseController;
use App\Models\Qh_Course;
use App\Models\Qh_Knowledge;
use Illuminate\Http\JsonResponse;

class CourseController extends ApiBaseController
{

    public function show($id) {
        $course = Qh_Course::findOrFail($id);

        // 知识点，两级
        //$course->knowledge = Qh_Knowledge::where('courseId', $course->id)
        //    ->where('level', 1)
        //    ->with(['children' => function($q) {
        //        $q->select('*')->where('level', 2)->orderByRaw('sort asc, id asc');
        //    }])->orderByRaw('sort asc, id asc')->get();
        // 知识点，一级
        $course->knowledge = Qh_Knowledge::selectRaw('id, courseId, name, kaoshiMode, kaoshiScore, kaoshiSeconds, questionCount, level')
            ->where('courseId', $course->id)
            ->where('level', 1)
            ->orderByRaw('sort asc, id asc')->get();

        return $this->restful($course);
    }

    /**
     * 获取知识点子级（小节）
     * @param $id
     * @return JsonResponse|object
     */
    public function getKnowledge2($id) {
        $knowledge = Qh_Knowledge::selectRaw('id, courseId, parentId, name, kaoshiMode, kaoshiScore, kaoshiSeconds, questionCount')
            ->where('parentId', $id)
            ->where('level', 2)
            ->orderByRaw('sort asc, id asc')->get();

        return $this->restful($knowledge);
    }
}
